<?php

class Main extends CI_Controller{
	function __construct() {
		//Call the Controller constructor
		parent::__construct();
	}
	
	//user selects movie, theater or showtime
	public function index(){
		$data['main'] = 'welcome.php';
		
		ini_set('error_reporting', E_ALL);
		
		$this->load->model('movie_model');
		$queryMovie = $this->movie_model->show_movies();
		$data['queryMovie'] = $queryMovie;
		
		$this->load->model('theater_model');
		$queryTheater = $this->theater_model->show_theaters();
		$data['queryTheater'] = $queryTheater;
		
		$this->load->model('showtime_model');
		$queryShowtime = $this->showtime_model->show_showtimes();
		$data['queryShowtime'] = $queryShowtime;
		
		$this->load->view('index', $data);
	}
	
	//user sees schedule and chooses one movie to buy
	//button search on welcome redirects to here.
	public function search_result(){
		//get values from  the form on welcome
		$info = array(
				'movie'					=> $_POST['textMovie'],
				'theater'				=> $_POST['textTheater'],
				'date'					=> $_POST['textDate'] // campo na view
		);
			
		//load model
		$this->load->model('showtime_model');
		$timeSelection = $this->showtime_model->get_showtimes($info);
		
		$data['timeSelection'] = $timeSelection;
		$data['main'] = "schedule.php";
		$this->load->view('index',$data);
	}
	
	//user selects seats
	public function choose_seat(){
		
		$info = $_POST['showtimeId'];
		
		$this->load->model('showtime_model');
		$showtimeSelection = $this->showtime_model->get($info);

		$this->load->model('ticket_model');
		$seatsAvailable = $this->ticket_model->get_seats($info);
		
		$data['showtime'] = $showtimeSelection;
		$data['seats'] = $seatsAvailable;
		$data['main'] = 'seats.php';
		$this->load->view('index', $data);
	}
	
	//loads payment form
	public function payment(){		
		
		$info= array(
				'seat' =>$_POST['seatId'],
				'showtime' => $_POST['showtimeId']
		);
		
		if($info['seat'] == 0) {
			$this->load->model('showtime_model');
			$showtimeId = $info['showtime'];
			$showtimeSelection = $this->showtime_model->get($showtimeId);
			
			$this->load->model('ticket_model');
			$seatsAvailable = $this->ticket_model->get_seats($showtimeId);
			
			$data['showtime'] = $showtimeSelection;
			$data['seats'] = $seatsAvailable;
			$data['main'] = 'seats.php';
			$this->load->view('index', $data);
		} else {
			$data['info'] = $info;
			$data['main'] = 'payment.php';
			$this->load->view('index', $data);
		}
	}
	
	//passar seat id e showtime id 
	//user pays for the ticket
	public function insert_payment_info(){
		$info = array(
			'first'					=> $_POST['inFirstName'],
			'last'					=> $_POST['inLastName'],
			'creditcardnumber'		=> $_POST['inCreditCard'], // campo na view
			'creditcardexpiration'	=> $_POST['inDate'],
			'showtime' => $_POST['showtimeId'],
			'seat' => $_POST['seatId']
		);
		
		$data['info'] = $info;
		
		$error = false;
		$errormsg_first = "";
		$errormsg_last = "";
		$errormsg_ccn = "";
		$errormsg_cce = "";
		$errormsg_lenccn = "";
		$errormsg_lencce = "";
		$errormsg_numccn = "";
		$errormsg_numcce = "";
		$errormsg_mm = "";
		$errormsg_yy = "";
		$errormsg_trans = "";
		
		if(!isset($_REQUEST['inFirstName']) || strlen($_REQUEST['inFirstName']) == 0) {
			$error = true;
			$errormsg_first = 'First name is required.';
		}
		
		if(!isset($_REQUEST['inLastName']) || strlen($_REQUEST['inLastName']) == 0) {
			$error = true;
			$errormsg_last = 'Last name is required.';
		}
		
		if($error == false && (!is_numeric($_REQUEST['inCreditCard']))) {
			$error = true;
			$errormsg_numccn = 'Credit card number should be only numbers.';
		}
		
		if($error == false && (!is_numeric($_REQUEST['inDate']))) {
			$error = true;
			$errormsg_numcce = 'Credit card expiration date should be only numbers.';
		} 
		
		if($error == false && (is_numeric($_REQUEST['inDate']))) {
			$year = substr($_POST['inDate'], 2, 2);
			$month = substr($_POST['inDate'], 0, 2);
			
			$yy = intval($year);
			$mm = intval($month);
			
			if($yy < intval(date("y"))) {
				$error = true;
				$errormsg_yy = 'Credit card expired.';
			} else if($yy == intval(date("y"))) {
				if($mm == 12 && $mm ==  intval(date("m"))) {
					$error = true;
					$errormsg_mm = 'Credit card expired.';
				} else if($mm <=  intval(date("m"))) {
					$error = true;
					$errormsg_mm = 'Credit card expired.';
				} else if($mm >= 13) {
					$error = true;
					$errormsg_mm = 'Credit card expired.';
				}
			} else if($yy > intval(date("y")) + 15) {
				$error = true;
				$errormsg_yy = 'Expiration year must be a valid year of expiration';
			} else if($mm < 1 || $mm > 12) {
				$error = true;
				$errormsg_mm = 'Month must be between 01 and 12.';
			}
		}
		
		if(!isset($_REQUEST['inCreditCard']) || strlen($_REQUEST['inCreditCard']) == 0) {
			$error = true;
			$errormsg_ccn = 'Credit card number is required.';
		}
		
		if(!isset($_REQUEST['inDate']) || strlen($_REQUEST['inDate']) == 0) {
			$error = true;
			$errormsg_cce = 'Credit card expiration date is required.';
		}
		
		if($error == false && strlen($_REQUEST['inCreditCard']) < 16) {
			$error = true;
			$errormsg_lenccn = 'Credit card number should be 16 characters long. Only numbers.';
		}
		
		if($error == false && strlen($_REQUEST['inDate']) < 4) {
			$error = true;
			$errormsg_lencce = 'Credit card expiration date should be 4 characters long. Only numbers.';
		}
		
		if($error == false) {
			$this->load->model('ticket_model');
			$this->load->model('showtime_model');
			
			$this->db->trans_begin();
			
			$this->ticket_model->insert($info);
			
			$ticket = $this->ticket_model->get_id($info);
			$data['ticket'] = $ticket;
		
			$this->showtime_model->update_availability($info['showtime']);
			
			if($this->db->trans_status() == FALSE) {
				$errormsg_trans = $errormsg_trans . $this->db->_error_message();
				$data["errormsg_trans"] = $errormsg_trans;
				$this->db->trans_rollback();
				$data['main'] = 'payment.php';
			} else {
				$this->db->trans_commit();
				$data['main'] = "thank_you.php";
			}
		} else {
			$data['error'] = $error;
			$data['errormsg_first'] = $errormsg_first;
			$data['errormsg_last'] = $errormsg_last;
			$data['errormsg_ccn'] = $errormsg_ccn;
			$data['errormsg_numccn'] = $errormsg_numccn;
			$data['errormsg_lenccn'] = $errormsg_lenccn;
			$data['errormsg_cce'] = $errormsg_cce;
			$data['errormsg_numcce'] = $errormsg_numcce;
			$data['errormsg_lencce'] = $errormsg_lencce;
			$data['errormsg_mm'] = $errormsg_mm;
			$data['errormsg_yy'] = $errormsg_yy;
			$data['errormsg_trans'] = $errormsg_trans;
 			
			$data['main'] = 'payment.php';
		}
		
		$this->load->view('index', $data);
	}
	
	//query result
	//user can print ticket and receipt
	public function ticket(){
		//got from thank you view
		$info= array(
				'showtime' => $_POST['showtimeId'],
				'ticket'	=> $_POST['ticketId']
		);
		
		//loads model 
		$this->load->model('showtime_model');
		//passes info for the model (showtimeId)
		$showtimeSelection = $this->showtime_model->get($info['showtime']);
		$data['showtime'] = $showtimeSelection;
		
		//load model
		$this->load->model('ticket_model');
		//passes info for the model(ticketId)
		$data['ticket']=$this->ticket_model->get($info['ticket']);
		
		$data['main'] = "summary.php";
		$this->load->view('index',$data);
	}
}

?>